import {
  ApiRootUrl,
  apiList
} from '../../../config/index.js'
import Dialog from '../../../@vant/weapp/dialog/dialog';
import {
  IMG_PATH
} from '../../../config/index.js'
import {
  request,formatDateTime,formatStringTime
} from "../../../utils/util.js"
import Toast from '../../../@vant/weapp/toast/toast';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    birthday: '',
    total: 0,
    isRange:false,
    pageNumber: 1,
    pageSize: 20,
    labourInfo:app.globalData.labourInfo,
    currentDate: new Date(2010, 1, 1).getTime(),
    lizhiminDate: new Date(2010, 1, 1).getTime(),
    lizhimaxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      } else if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    lizhiTimeShow: false,
    lizhiTime: '请选择',
    zhusuShowColumns: ['全部','已入宿', '未入宿'],
    zhusuShow: false,
    tongzhiShow: false,
    allpendingchecked: false,
    allchecked: false,
    tongzhimessage: '',
    height:0,
    allIsRoomType:0,
    determineList: [],
    pendingList: [],
    onsuList: [],
    awaysuList: [],
    minDate: new Date(2020, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    sourceShow: false,
    dangerShow: false,
    timeshow: false,
    statusBarHeight: '',
    active: 0,
    searchValue: '',
    sourceName: '是否住宿',
    iszhusu: '是否住宿',
    time: '选择时间',
    leaveTimeStart: "",
    leaveTimeEnd: "",
    danger: '备案登记',
    dangerId: "",
    sourceShowColumns: [{
      id: '0',
      text: '全部'
    }, {
      id: '1',
      text: '已住宿'
    }, {
      id: '2',
      text: '未住宿'
    }],
    dangerShowColumns: [{
      id: '',
      text: '全部'
    },{
      id: '0',
      text: '未提交'
    }, {
      id: '1',
      text: '已提交'
    }, {
      id: '2',
      text: '备案中'
    }, {
      id: '3',
      text: '已备案'
    }],
    tabList: [{
      title: '在职人员',
    }, {
      title: '在宿人员',
    }, {
      title: '离宿人员',
    }, {
      title: '离职人员',
    }]
  },
  companyClick() {
    this.setData({
      zhusuShow: true
    })
  },
  dangerShowClose() {
    this.setData({
      dangerShow: false
    })
  },
  zhusuShowClose() {
    console.log("-----")
    this.setData({
      zhusuShow: false
    })
  },
  zhusuClick() {
    this.setData({
      zhusuShow: true
    })
  },
  dangerClick() {
    this.setData({
      dangerShow: true
    })
  },
  getSearchValue(e) {
    let value = e.detail;
    this.setData({
      searchValue: value
    }, () => {
      if(Number(this.data.active) == 0 ){
        this.getOnJob(this.data.allIsRoomType)
      }
      if(Number(this.data.active) == 1 ){
        this.getOnJob(1)
      }
      if(Number(this.data.active) == 2 ){
        //已离宿
        this.getLeaveRoom()
      }
      if(Number(this.data.active) == 3 ){
        //已离职
        this.getLeaveJob()
      } 
    })
  },
  sourceShowClose() {
    this.setData({
      sourceShow: false
    })
  },
  zhusuShowConfirm(event) {
    console.log("-------->",event.detail.value)
    this.setData({
      iszhusu: event.detail.value.id,
      sourceName: event.detail.value.text,
      zhusuShow: false
    },()=>{
      let type = event.detail.value.id;
      this.getOnJob(event.detail.value.id)
      this.setData({
        allIsRoomType:type
      })
    })
  },
  dangerShowConfirm(event) {
    this.setData({
      danger: event.detail.value.text,
      dangerId: event.detail.value.id,
      dangerShow: false,
      pageNumber:1
    },()=>{
      if(Number(this.data.active) == 0 ){
        this.getOnJob(this.data.allIsRoomType)
      }
      if(Number(this.data.active) == 1 ){
        this.getOnJob(1)
      }
      if(Number(this.data.active) == 2 ){
        //已离宿
        this.getLeaveRoom()
      }
      if(Number(this.data.active) == 3 ){
        //已离职
        this.getLeaveJob()
      } 
    })
  },
  timeClick() {
    this.myComponent.showPicker()
    this.setData({
      timeshow: true
    })
  },
  getRangeTime(e){
    let beginTime = formatDateTime(e.detail.begin);
    let endTime =formatDateTime(e.detail.end);
    console.log(beginTime,endTime)
    this.setData({
      leaveTimeStart:formatStringTime(beginTime),
      leaveTimeEnd:formatStringTime(endTime),
      isRange:true
    })
  },
  formateTime(time) {
    let arr = time.split("-")
    return arr[0] + arr[1] + arr[2] + ""
  },
  selectStart(e) {
    let time = this.formateTime(e.detail)
    console.log("开始时间", e)
    this.setData({
      time: `${e.detail} - ${this.data.leaveTimeEnd}`,
      leaveTimeStart: time
    })
  },
  selectEnd(e) {
    let time = this.formateTime(e.detail)
    console.log("结束时间", e)
    this.setData({
      time: `${this.data.leaveTimeStart} - ${e.detail}`,
      leaveTimeEnd: time
    })
  },
  defalut() {
    //触发简历初始化，去除默认选择项
    const child = this.selectComponent('#Calendar');
    const child1 = this.selectComponent('#Calendar1');
    child.resetToday()
    child1.resetToday()
  },
  timeAll() {
    this.defalut()
    this.setData({
      timeshow: false,
      leaveTimeStart: "",
      leaveTimeEnd: "",
      time: "选择时间",
      pageNumber:1
    }, () => {
      if(Number(this.data.active) == 0 ){
        this.getOnJob(this.data.allIsRoomType)
      }
      if(Number(this.data.active) == 1 ){
        this.getOnJob(1)
      }
      if(Number(this.data.active) == 2 ){
        //已离宿
        this.getLeaveRoom()
      }
      if(Number(this.data.active) == 3 ){
        //已离职
        this.getLeaveJob()
      } 
    });
  },
  sourceShowConfirm(event) {
    console.log("............")
    this.setData({
      sourceName: event.detail.value.text,
      sourceShow: false
    })
  },
  tabClick(event) {
    this.setData({
      active: event.detail.index,
      searchValue: "",
      pageNumber:1
    },()=>{
      if(Number(event.detail.index) == 0 ){
        this.getOnJob(this.data.allIsRoomType)
      }
      if(Number(event.detail.index) == 1 ){
        this.getOnJob(1)
      }
      if(Number(event.detail.index) == 2 ){
        //已离宿
        this.getLeaveRoom()
      }
      if(Number(event.detail.index) == 3 ){
        //已离职
        this.getLeaveJob()
      } 
    })
  },
  birthdadInput(event) {
    let time = this.formatDate(event.detail);
    console.log(time);
    this.setData({
      birthday: time,
    });
  },
  lizhiTimeClose() {
    this.setData({
      lizhiTimeShow: false
    })
  },
  familybirthdayConfirm(event) {
    console.log(event);
    let birthday = this.data.birthday;
    this.setData({
      lizhiTime: birthday,
      lizhiTimeShow: false,
      tongzhiShow: true
    })
  },
  lizhiTimeClick() {
    this.setData({
      lizhiTimeShow: true,
      tongzhiShow: false
    })
  },
  backClick(event) {
    console.log(event);
    wx.navigateBack();
    // Dialog.confirm({
    //   title: '返回',
    //   message: '请确认是否返回？',
    // }).then(() => {
    //   wx.navigateBack();
    // }).catch(() => {
    //   console.log('取消')
    // });
  },
  formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
  },
  timeConfirm(event) {

    let beginTime = this.data.leaveTimeStart
    let endTime = this.data.leaveTimeEnd
    let time = beginTime+"-"+endTime
    if(!this.data.isRange){
      beginTime = "";
      endTime = "";
      time="选择时间"
    }
    this.setData({
      timeshow: false,
      leaveTimeStart:beginTime,
      leaveTimeEnd:endTime,
      time:time,
      isRange:false
    },()=>{
      if(Number(this.data.active) == 0 ){
        this.getOnJob(this.data.allIsRoomType)
      }
      if(Number(this.data.active) == 1 ){
        this.getOnJob(1)
      }
      if(Number(this.data.active) == 2 ){
        //已离宿
        this.getLeaveRoom()
      }
      if(Number(this.data.active) == 3 ){
        //已离职
        this.getLeaveJob()
      } 
    });
    this.myComponent.hidePicker()


  },
  refendClick(event) {
    console.log(event);
    this.setData({
      tongzhiShow: true
    })
  },
  refendpendingClick(event) {
    console.log(event);
  },
  pendingChange(event) {
    let id = event.target.dataset.id;
    let dataList = this.data.determineList;
    dataList.forEach((item) => {
      if (item.id == id) {
        item.checked = event.detail
      }
    })
    this.setData({
      determineList: dataList
    })
    if (event.detail) {
      this.setData({
        allpendingchecked: true
      })
    } else {
      let list = dataList.map(value => value['checked']);
      if (list.includes(true)) {
        this.setData({
          allpendingchecked: true
        })
      } else {
        this.setData({
          allpendingchecked: false
        })
      }

    }
  },
  dengjiClick() {
    this.setData({
      tongzhiShow: true
    })
  },
  pendingDetailsClick(event) {
    console.log(event);
    wx.navigateTo({
      url: `../detail/detail?id=${event.target.dataset.id}&type=${event.target.dataset.type}`
    })
  },
  refendpendingClick(event) {
    wx.navigateTo({
      url: `../pendingdetails/pendingdetails?id=${event.target.dataset.id}`
    })
  },
  passChange(event) {
    console.log(event);
    let id = event.target.dataset.id;
    let dataList = this.data.passList;
    dataList.forEach((item) => {
      if (item.id == id) {
        item.checked = event.detail
      }
    })
    this.setData({
      passList: dataList
    })
  },
  timeClose() {
    this.myComponent.hidePicker()
    this.setData({
      timeshow: false
    });
  },
  pendingallChange(event) {
    let dataList = this.data.determineList;
    dataList.forEach((item) => {
      item.checked = event.detail;
    })
    this.setData({
      allpendingchecked: event.detail,
      determineList: dataList
    })
  },
  passallChange(event) {
    let dataList = this.data.passList;
    dataList.forEach((item) => {
      item.checked = event.detail;
    })
    this.setData({
      allchecked: event.detail,
      passList: dataList
    })
  },
  tongzhiClose() {
    this.setData({
      tongzhiShow: false,
      tongzhimessage: ''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let query = wx.createSelectorQuery();
    query.select('.content').boundingClientRect(rect => {
      let height = rect.height;
      this.setData({
        statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
        active: Number(options.id),
        height: wx.getSystemInfoSync().windowHeight - height,
        labourInfo:app.globalData.labourInfo
      })
    }).exec();
    this.setData({
      active:Number(options.id)
    })
    this.myComponent = this.selectComponent('#timeRangeComponent')
    if(Number(options.id) == 0 ){
      this.getOnJob(this.data.allIsRoomType)
    }
    if(Number(options.id) == 1 ){
      this.getOnJob(1)
    }
    if(Number(options.id) == 2 ){
      //已离宿
      this.getLeaveRoom()
    }
    if(Number(options.id) == 3 ){
      //已离职
      this.getLeaveJob()
    } 
    // this.getOnJob(this.data.allIsRoomType)
  },
  
  getOnJob(type) {
    console.log("type======>",type)
    console.log(this.data.active);
    let params = {
      // labourCode: this.data.sourceCode,
      labourCode:app.globalData.labourInfo.code,
      name: this.data.searchValue,
      phone: "",
      dormStatus:type == 0 ? null :type == 1 ? 2 :type == 2 ? 0 : null,
      surveyStatus: this.data.dangerId == ""?"":Number(this.data.dangerId),
      pageNumber:1,
      pageSize:20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.onJob, params).then((res) => {
     console.log("请求结果=====>",res)
     if(this.data.active == 1){
       let arr = []
       for(let i = 0; i < res.data.list.length; i++ ){
        res.data.list[i].checked = false
        arr.push(res.data.list[i])
       }
      this.setData({
        total:res.data.total,
        onsuList:arr
      })
     }else{
      this.setData({
        total:res.data.total,
        pendingList:res.data.list
      })
     }
      
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  getLeaveRoom() {
    let params = {
      labourCode: app.globalData.labourInfo.code,
      name: this.data.searchValue,
      phone: "",
      startTime: this.data.leaveTimeStart,
      endTime: this.data.leaveTimeEnd,
      surveyStatus: this.data.dangerId == ""?"":Number(this.data.dangerId),
      pageNumber: 1,
      pageSize: 20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.leaveRoom, params).then((res) => {
      console.log("请求结果=====>", res)
      let arr = []
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i].checked = false
          arr.push(res.data.list[i])
        }
        this.setData({
          total: res.data.total,
          awaysuList: arr
        })
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  getLeaveJob() {
    let params = {
      labourCode: app.globalData.labourInfo.code,
      // labourCode:"002",
      name: this.data.searchValue,
      phone: "",
      leaveTimeStart:this.data.leaveTimeStart,
      leaveTimeEnd:this.data.leaveTimeEnd,
      surveyStatus: this.data.dangerId== ""?"":Number(this.data.dangerId),
      pageNumber:1,
      pageSize:20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.leaveJob, params).then((res) => {
     console.log("请求结果=====>",res)
     this.setData({
      determineList:res.data.list,
      total:res.data.total
     })    
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  getOnJobPage(type) {
    console.log("type======>",type)
    let params = {
      // labourCode: this.data.sourceCode,
      labourCode:app.globalData.labourInfo.code,
      name: this.data.searchValue,
      phone: "",
      dormStatus:type == 0 ? null :type == 1 ? 2 :type == 2 ? 0 : null,
      surveyStatus: this.data.dangerId == ""?"":Number(this.data.dangerId),
      pageNumber:this.data.pageNumber+1,
      pageSize:20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.onJob, params).then((res) => {
     console.log("请求结果=====>",res)
     if(this.data.active == 1){
      let arr = []
      for(let i = 0; i < res.data.list.length; i++ ){
       res.data.list[i].checked = false
       arr.push(res.data.list[i])
      }
     this.setData({
       total:res.data.total,
       onsuList:this.data.onsuList.concat(arr),
       pageNumber:this.data.pageNumber + 1,
       isLoad:false,
     })
    }else{
     this.setData({
       total:res.data.total,
       pageNumber:this.data.pageNumber + 1,
       isLoad:false,
       pendingList:this.data.pendingList.concat(res.data.list)
     })
    }
      
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  getLeaveRoomPage(){
    let params = {
      labourCode:app.globalData.labourInfo.code,
      name: this.data.searchValue,
      phone: "",
      startTime: this.data.leaveTimeStart,
      endTime: this.data.leaveTimeEnd,
      surveyStatus: this.data.dangerId == ""?"":Number(this.data.dangerId),
      pageNumber: this.data.pageNumber + 1,
      pageSize: 20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.leaveRoom, params).then((res) => {
      console.log("请求结果=====>", res)
      let arr = []
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i].checked = false
          arr.push(res.data.list[i])
        }
        this.setData({
          total: res.data.total,
          pageNumber:this.data.pageNumber + 1,
          isLoad:false,
          awaysuList: this.data.awaysuList.concat(arr),
          
        })
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  getLeaveJobPage(){
    let params = {
      labourCode: app.globalData.labourInfo.code,
      // labourCode:"002",
      name: this.data.searchValue,
      phone: "",
      leaveTimeStart:this.data.leaveTimeStart,
      leaveTimeEnd:this.data.leaveTimeEnd,
      surveyStatus: this.data.dangerId== ""?"":Number(this.data.dangerId),
      pageNumber:this.data.pageNumber + 1,
      pageSize:20,
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.leaveJob, params).then((res) => {
     console.log("请求结果=====>",res)
     this.setData({
      determineList:this.data.determineList.concat(res.data.list),
      total:res.data.total,
      pageNumber:this.data.pageNumber + 1,
      isLoad:false
     })    
      Toast.clear();
    }).catch((err) => {
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  loadMore(e){
    //分页加载
    if(Number(this.data.active) == 0){
      if(this.data.pendingList.length < this.data.total){
        this.setData({
          isLoad:true
        })
        this.getOnJobPage(this.data.allIsRoomType)
      }
    }
    if(Number(this.data.active) == 1){
      if(this.data.onsuList.length < this.data.total){
        this.setData({
          isLoad:true
        })
        this.getOnJobPage(1)
      }
    }
    if(Number(this.data.active) == 2){
      if(this.data.awaysuList.length < this.data.total){
        this.setData({
          isLoad:true
        })
      this.getLeaveRoomPage()
      }
    }
    if(Number(this.data.active) == 3){
      if(this.data.determineList.length < this.data.total){
        this.setData({
          isLoad:true
        })
        this.getLeaveJobPage()
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})